<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <FullScreen
        v-model="showDialog"
        title="建账出库单明细"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button v-if="canUpdate" @click="update">修改</el-button>
          <el-button v-if="canCancelDown" @click="cancelDown"
            >取消拣货</el-button
          >
          <el-button v-if="canCancel" @click="cancel">取消</el-button>
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="
              $permission('accountExportOrder:print') &&
              infoList.data.status !== 1
            "
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <el-button
            v-if="
              $permission('accountExportOrder:exportExcel') &&
              infoList.data.status !== 1
            "
            @click="exportExcel"
            >导出excel</el-button
          >

          <el-button v-if="canSubmit" @click="submit">提交</el-button>
          <el-button v-if="canRevoke" @click="revoke">撤回</el-button>
          <el-button v-if="canExecute" @click="otherExport">出库</el-button>
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <BasicInformation :info="infoList" />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <PageTable
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                :check-box="false"
                :tab-index="true"
                :summary-field-list="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :isDynamic="true"
              >
              </PageTable>
            </div>
          </div>
        </div>
      </FullScreen>
    </div>
    <OtherExportOrderModify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :orderId="modifyInfo.exportId"
      @returnModify="returnModify"
    />
    <IndividualDetails
      v-if="individualInfo.visible"
      v-model:show-dialog="individualInfo.visible"
      v-model:query="individualInfo.query"
      :info="individualInfo.info"
    />
  </div>
</template>

<script setup name="accountExportOrderDetail">
import {
  getAccountExportOrderById,
  exportExcelAccountExportOrder,
  getAccountExportOrderPrint,
  accountExportOrderDetailCustomizeListAll,
  submitAccountExportOrder,
  exportAccountExportOrder,
  revokeAccountExportOrder,
  accountExportOrdersCancel,
  accountExportOrdersCancelDownShelf,
} from '@/api/beginning.js'
import { getCustomizeSetting } from '@/api/customize'
import { orderAffairAll } from '@/api/affair'
import { fatchTemplateByCode } from '@/api/base'
import OtherExportOrderModify from './modify'
import IndividualDetails from './IndividualDetails'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'
const props = defineProps(['visible', 'id', 'tableRow'])
const { proxy } = getCurrentInstance()
const infoList = ref({
  data: {},
  fieldList: [
    { label: '出库单号', value: 'orderNo', type: 'text' },
    { label: '出库仓库', value: 'depotName', type: 'text' },
    { label: '出库部门', value: 'deptName', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '出库人', value: 'exportUserName', type: 'text' },
    { label: '出库时间', value: 'exportTime', type: 'text' },
    { label: '创建人', value: 'createUserName', type: 'text' },
    { label: '创建时间', value: 'createTime', type: 'text' },
    { label: '更新人', value: 'updateUserName', type: 'text' },
    { label: '更新时间', value: 'updateTime', type: 'text' },
    {
      label: '是否寄售业务',
      value: 'isConsignBusinessValue',
      type: 'text',
      show: proxy.$store.state.permission.systemConfig.is_consign_business == 1,
    },
    { label: '备注', value: 'remark', type: 'popover' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.tableRow.id,
      },
    },
    queryParams: {},
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '个体详情',
        event: showDetail,
        show: proxy.$permission('accountExportOrder:IndividualDetails'),
        ifRender: ifIndividualDetails,
      },
    ],
  },
})
const affairTableInfo = ref({
  query: {
    orderType: 91,
    orderId: props.tableRow.id,
    sort: 'operate_time desc',
  },
})
const individualInfo = ref({
  visible: false,
  query: {},
  info: {},
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    proxy.$emit('update:visible', val) // 更新父组件visible
  },
})

const canUpdate = computed(() => {
  return (
    proxy.$permission('accountExportOrder:update') &&
    infoList.value.data.status === 1
  )
})
const canCancelDown = computed(() => {
  return (
    proxy.$permission('accountExportOrder:cancelDown') &&
    infoList.value.data.status === 4 &&
    infoList.value.data.isCargoSpaceManage === 1
  )
})
const canSubmit = computed(() => {
  return (
    proxy.$permission('accountExportOrder:submit') &&
    infoList.value.data.status === 1
  )
})
const canRevoke = computed(() => {
  return (
    proxy.$permission('accountExportOrder:revoke') &&
    infoList.value.data.status === 2 &&
    infoList.value.data.isCargoSpaceManage === 0
  )
})
const canCancel = computed(() => {
  return (
    proxy.$permission('accountExportOrder:cancel') &&
    infoList.value.data.status === 2 &&
    infoList.value.data.isCargoSpaceManage === 1
  )
})
const canExecute = computed(() => {
  return (
    proxy.$permission('accountExportOrder:export') &&
    infoList.value.data.status === 2
  )
})
watch(
  () => props.visible,
  (val) => {
    showDialog.value = val
  },
  { immediate: true }
)
function ifIndividualDetails(row) {
  return (
    infoList.value.data.isCargoSpaceManage === 0 ||
    (infoList.value.data.isCargoSpaceManage === 1 && row.status === 3)
  )
}
function returnModify() {
  getMain()
  getList()
}
function cancelDown() {
  accountExportOrdersCancelDownShelf(infoList.value.data.id).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function cancel() {
  accountExportOrdersCancel(infoList.value.data.id).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
//提交
function submit() {
  let ids = []
  ids.push(infoList.value.data.id)
  submitAccountExportOrder(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function revoke() {
  let ids = []
  ids.push(infoList.value.data.id)
  revokeAccountExportOrder(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function otherExport() {
  let ids = []
  ids.push(infoList.value.data.id)
  exportAccountExportOrder(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}

function returnBack() {
  proxy.$emit('returnBack', false)
}
function showDetail(row) {
  individualInfo.value.visible = true
  individualInfo.value.info = row
  individualInfo.value.query = {
    id: {
      queryType: 'IN',
      queryCategory: 'AND',
      value: row.groupConcatIds,
    },
    orderId: {
      queryType: 'EQUALS',
      queryCategory: 'AND',
      value: props.tableRow.id,
    },
  }
}
function getMain() {
  getAccountExportOrderById(props.tableRow.id).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
function getList() {
  accountExportOrderDetailCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}

function update() {
  modifyInfo.value.exportId = infoList.value.data.id
  modifyInfo.value.title = '修改建账出库单'
  modifyInfo.value.visible = true
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'accountExportOrderDetail',
    deptId: infoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            applicationId: infoList.value.data.id,
            orderId: infoList.value.data.id,
            orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      applicationId: infoList.value.data.id,
      orderId: infoList.value.data.id,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function printCallback() {}
//导出excel
function exportExcel() {
  exportExcelAccountExportOrder(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function getCustomize() {
  let param = {
    tableKey:
      'beginningAccountExportOrderDetail-com.slip.spd.entity.vo.account.AccountExportOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = obj.fieldList
      getMain()
      getList()
    }
  })
}
let obj = {
  price: {
    label: '单价',
    value: 'price',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('accountExportOrder:price'),
    show: false,
    hidden: true,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('accountExportOrder:price'),
    show: false,
    hidden: true,
  },
  orderNo: {
    label: '单号',
    value: 'orderNo',
    componentType: 'input',
    show: false,
    type: 'column',
    hidden: true,
  },
  defaultSupplierPrice: {
    label: '默认供应商价格',
    value: 'defaultSupplierPrice',
    type: 'column',
    permission: !proxy.$permission('accountExportOrder:price'),
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>

<style scoped></style>
